home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / roots / fsolver.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-22  |  2.6 KB  |  108 lines

  1. /* roots/fsolver.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Reid Priedhorsky, Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <gsl/gsl_errno.h>
  24. #include <gsl/gsl_roots.h>
  25.  
  26. gsl_root_fsolver *
  27. gsl_root_fsolver_alloc (const gsl_root_fsolver_type * T)
  28. {
  29.   gsl_root_fsolver * s = (gsl_root_fsolver *) malloc (sizeof (gsl_root_fsolver));
  30.  
  31.   if (s == 0)
  32.     {
  33.       GSL_ERROR_VAL ("failed to allocate space for root solver struct",
  34.             GSL_ENOMEM, 0);
  35.     };
  36.  
  37.   s->state = malloc (T->size);
  38.  
  39.   if (s->state == 0)
  40.     {
  41.       free (s);        /* exception in constructor, avoid memory leak */
  42.  
  43.       GSL_ERROR_VAL ("failed to allocate space for root solver state",
  44.             GSL_ENOMEM, 0);
  45.     };
  46.  
  47.   s->type = T ;
  48.   s->function = NULL ;
  49.  
  50.   return s;
  51. }
  52.  
  53. int
  54. gsl_root_fsolver_set (gsl_root_fsolver * s, gsl_function * f, double x_lower, double x_upper)
  55. {
  56.   if (x_lower > x_upper)
  57.     {
  58.       GSL_ERROR ("invalid interval (lower > upper)", GSL_EINVAL);
  59.     }
  60.  
  61.   s->function = f;
  62.   s->root = 0.5 * (x_lower + x_upper);  /* initial estimate */
  63.   s->x_lower = x_lower;
  64.   s->x_upper = x_upper;
  65.  
  66.   return (s->type->set) (s->state, s->function, &(s->root), x_lower, x_upper);
  67. }
  68.  
  69. int
  70. gsl_root_fsolver_iterate (gsl_root_fsolver * s)
  71. {
  72.   return (s->type->iterate) (s->state, 
  73.                  s->function, &(s->root), 
  74.                              &(s->x_lower), &(s->x_upper));
  75. }
  76.  
  77. void
  78. gsl_root_fsolver_free (gsl_root_fsolver * s)
  79. {
  80.   free (s->state);
  81.   free (s);
  82. }
  83.  
  84. const char *
  85. gsl_root_fsolver_name (const gsl_root_fsolver * s)
  86. {
  87.   return s->type->name;
  88. }
  89.  
  90. double
  91. gsl_root_fsolver_root (const gsl_root_fsolver * s)
  92. {
  93.   return s->root;
  94. }
  95.  
  96. double
  97. gsl_root_fsolver_x_lower (const gsl_root_fsolver * s)
  98. {
  99.   return s->x_lower;
  100. }
  101.  
  102. double
  103. gsl_root_fsolver_x_upper (const gsl_root_fsolver * s)
  104. {
  105.   return s->x_upper;
  106. }
  107.  
  108.